package sd.czw.com.miemiegoushop.activity;

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import sd.czw.com.miemiegoushop.R;
import sd.czw.com.miemiegoushop.base.CzwBaseActivity;

/**
 * Created by czw
 * desc 描述：
 * on 2017/9/29.
 */
public class BottomSheetBehaviorActivity extends CzwBaseActivity {

    @Bind(R.id.banner)
    MZBannerView banner;
    @Bind(R.id.appbar)
    AppBarLayout appBarLayout;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.collbar)
    CollapsingToolbarLayout collapsingToolbarLayout;

    private List<String> path = new ArrayList<>();

    @Override
    public void onPause() {
        super.onPause();
        banner.pause();//暂停轮播
    }

    @Override
    public void onResume() {
        super.onResume();
        banner.start();//开始轮播
    }
    @Override
    public int getLayoutId() {
        return R.layout.activity_bottomsheet;
    }

    @Override
    public void initView() {
        path.add("http://img01.sogoucdn.com/app/a/100520024/dd4ec94c270a74353959ae4f976614ef");
        path.add("http://img04.sogoucdn.com/app/a/100520024/a453c4d03df29f3e952ce445a407aaf4");
        path.add("http://img04.sogoucdn.com/app/a/100520024/68f46a805ae8547428a149a3f6e0eeeb");
        path.add("http://img01.sogoucdn.com/app/a/100520024/f8661955ac5bff5fab4059309beb3f5e");



        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.item_search){
                    Toast.makeText(BottomSheetBehaviorActivity.this, "搜索", Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });


        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                Log.e("zhouwei","appbarHeight:"+appBarLayout.getHeight()+" getTotalScrollRange:"+appBarLayout.getTotalScrollRange()+" offSet:"+verticalOffset);
                if(Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()){
                    toolbar.setTitleTextColor(getResources().getColor(R.color.XFrame_white));
                    collapsingToolbarLayout.setTitle("哈哈哈");
                    toolbar.setNavigationIcon(R.mipmap.back);
                    toolbar.getNavigationIcon().setVisible(true,true);
                }else{
                    collapsingToolbarLayout.setTitle("");
                    toolbar.setNavigationIcon(R.mipmap.back);
                    toolbar.getNavigationIcon().setVisible(false,false);
                }
            }
        });
    }

    @Override
    public void initData() {
        banner.setDelayedTime(1000);
        // 设置数据
        banner.setPages(path, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });


    }
    public static class BannerViewHolder implements MZViewHolder<String> {
        private ImageView mImageView;
        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item,null);
            mImageView = (ImageView) view.findViewById(R.id.banner_image);
            return view;
        }

        @Override
        public void onBind(Context context, int position, String data) {
            // 数据绑定
            Glide.with(context).load(data).into(mImageView);
        }
    }
}
